package com.kgc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.mapper.MachiningMapper;
import com.kgc.service.IMachiningProviderService;
import entity.Machining;
import entity.ResponseMessage;
import entity.StatusCode;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;


/**
 *
 * @description:  加工工艺管理service 实现类
 *
 * @author: Cheng
 *
 * @create: 2022-04-18 17:58
 **/
@Service
@SuppressWarnings("all")
public class IMachiningProviderServiceImpl implements IMachiningProviderService {

    private Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private MachiningMapper machiningMapper;


    /**
     * 检查加工工艺是否同名
     * @param name
     * @return
     */
    @Override
    public ResponseMessage selectInsrtCheckName(String name) {
        logger.info("IMachiningProviderServiceImpl selectProductById is start .... param:name"+name);
        ResponseMessage responseMessage = new ResponseMessage();
        try {
            //更新商品信息
            int row = machiningMapper.selectInsrtCheckName(name);
            logger.debug("IMachiningProviderServiceImpl selectProductById result:row->"+row);
            if (row>0){
                logger.error("IMachiningProviderServiceImpl selectProductById is error , The name exist same name");
                responseMessage.setCode(StatusCode.ERROR);
                responseMessage.setMessage("加工工艺存名称在同名");
            }else {
                responseMessage.setMessage("加工工艺名称不存在同名");
                responseMessage.setCode(StatusCode.OK);
                responseMessage.setData(row);
                responseMessage.setFlag(true);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("IMachiningProviderServiceImpl selectProductById is error ",e);
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("IMachiningProviderServiceImpl selectProductById is error");
        }
        return responseMessage;
    }

    /**
     * 添加一条加工管理信息
     * @param machining
     * @return
     */
    @Override
    public ResponseMessage insertMachiningInfo(Machining machining) {
        logger.info("IMachiningProviderServiceImpl insertMachiningInfo is start .... param:machining"+machining);
        ResponseMessage responseMessage = new ResponseMessage();
        try {
            //更新商品信息
            int row = machiningMapper.insert(machining);
            logger.debug("IMachiningProviderServiceImpl insertMachiningInfo result:row->"+row);
            if (row>0){
                responseMessage.setMessage("添加加工工艺成功");
                responseMessage.setCode(StatusCode.OK);
                responseMessage.setData(row);
                responseMessage.setFlag(true);
            }else {
                logger.error("IMachiningProviderServiceImpl insertMachiningInfo is error");
                responseMessage.setCode(StatusCode.ERROR);
                responseMessage.setMessage("加工工艺存失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("IMachiningProviderServiceImpl insertMachiningInfo is error ",e);
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("IMachiningProviderServiceImpl insertMachiningInfo is error");
        }
        return responseMessage;
    }


    /**
     * 分页查询加工工艺信息
     * @param pageNum
     * @param name
     * @param sort
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage selectMachiningInfo(Integer pageNum, String name, String sort) {
        logger.info("IMachiningProviderServiceImpl selectMachiningInfo is start ....param:pageNum:-->"+pageNum + ",name:-->"+name + ",sort"+sort);
        ResponseMessage responseMessage = new ResponseMessage();
        HashMap<String, Object> map = new HashMap<>();
        //将参数存放在map当中
        map.put("name", name);
        map.put("sort", sort);
        try {
            PageHelper pageHelper = new PageHelper();
            pageHelper.startPage(pageNum, 5);
            //查询所有加工工艺管理信息
            List<Machining> machiningList = machiningMapper.selectMachiningInfo(map);
            PageInfo<Machining> machiningPageInfo = new PageInfo<>(machiningList);          //将查询的数据存放到pageInfo对象当中
            logger.debug("IMachiningProviderServiceImpl selectMachiningInfo result:machiningPageInfo->"+machiningPageInfo);
            if (machiningList==null || "".equals(machiningList)){       //查询数据为空
                logger.error("IMachiningProviderServiceImpl insertMachiningInfo is error ,result is null");
                responseMessage.setMessage("抱歉,查询无任何数据");
                responseMessage.setCode(StatusCode.ERROR);
            }else {
                responseMessage.setCode(StatusCode.OK);
                responseMessage.setMessage("加工工艺信息查询成功");
                responseMessage.setFlag(true);
                responseMessage.setData(machiningPageInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("IMachiningProviderServiceImpl insertMachiningInfo is error ",e);
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("IMachiningProviderServiceImpl insertMachiningInfo is error");
        }
        return responseMessage;
    }

    @Override
    public ResponseMessage selectMachiningById(String id) {
        logger.info("IMachiningProviderServiceImpl selectMachiningById is start....param:id-->" + id);
        ResponseMessage responseMessage = new ResponseMessage();
        try {
            Machining machining = machiningMapper.selectById(id);
            logger.debug("IMachiningProviderServiceImpl selectMachiningById result is machining:-->"+machining);
            if (machining==null) {
                logger.error("IMachiningProviderServiceImpl selectMachiningById reslut is null");
                return new ResponseMessage(false,StatusCode.ERROR,"IMachiningProviderServiceImpl selectMachiningById reslut is null",null);
            }
            responseMessage.setCode(StatusCode.OK);
            responseMessage.setFlag(true);
            responseMessage.setData(machining);
            responseMessage.setMessage("通过id查询加工工艺信息成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return responseMessage;
    }

    @Override
    public ResponseMessage updateMachiningInfo(Machining machining) {
        logger.info("IMachiningProviderServiceImpl updateMachiningInfo is start....param:machining-->" + machining);
        ResponseMessage responseMessage = new ResponseMessage();
        try {
            int i = machiningMapper.updateById(machining);
            logger.debug("IMachiningProviderServiceImpl updateMachiningInfo reslut : i" + i);
            if (i>0) {
                logger.debug("IMachiningProviderServiceImpl updateMachiningInfo result is machining:-->"+machining);
                responseMessage.setCode(StatusCode.OK);
                responseMessage.setFlag(true);
                responseMessage.setData(i);
                responseMessage.setMessage("更新加工工艺信息成功");
            }else{
                logger.error("IMachiningProviderServiceImpl updateMachiningInfo reslut is null");
                return new ResponseMessage(false,StatusCode.ERROR,"IMachiningProviderServiceImpl updateMachiningInfo reslut is null",null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return responseMessage;
    }
}
